<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Infrastructure\Context\Type;
use ArrayObject;

/**
 * Resolver that looks up an identifier's type using a map
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class MapResolver implements ResolverInterface
{
	/**
	 * @var ArrayObject<string:string>
	 */
	protected $typeMap;
	
	/**
	 * Class constructor
	 * @return void
	 */
	public function __construct()
	{
		$this->typeMap = new ArrayObject();
	}
	
	/**
	 * Resolves a context's type using a resolution context
	 * 
	 * @param ResolutionContext $resolution
	 * @return void
	 */
	public function resolve(ResolutionContext $resolution)
	{
		$identifier = $resolution->getIdentifier();
		if($this->typeMap->offsetExists($identifier)) {
			$resolvedType = $this->typeMap->offsetGet($identifier);
			
			$resolution->setResolvedType($resolvedType);
			$resolution->setResult(ResolutionResult::Resolved());
		}
	}
	
	/**
	 * Adds an identifier to type mapping
	 * 
	 * @param string $identifier
	 * @param string $type
	 * @return void
	 */
	public function add($identifier, $type)
	{
		$this->typeMap->offsetSet($identifier, $type);
	}
	
	/**
	 * Removes an identifier to type mapping
	 * 
	 * @param string $identifier
	 * @return void
	 */
	public function remove($identifier)
	{
		$this->typeMap->offsetUnset($identifier);
	}
}